/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import sun.tools.jar.resources.jar;

/**
 *
 * @author Administrador
 */
public class operaciones {

    vistaa ass;

    public operaciones(vistaa a) {
        ass = a;

    }

    public void bajaruno(String URLL) {
        ass.bajadoo("EMPEZANDO");
        ass.update(ass.getGraphics());
        //  jTextField1.setText("http://submanga.com/Gantz/306/50111");
        //jTextField1.setText("http://submanga.com/c/5789");
        //    jTextField1.setText("http://submanga.com/c/22281");
        //jTextField1.setText("http://submanga.com/Bakuman/59/49598");
        String cadNec = "";
        String carpeta = "";
        try {
            ass.update(ass.getGraphics());

            // TODO code application logic here
            //aki pasar el manga
            URL url = new URL(URLL);
            // Volcamos lo recibido al buffer
            ass.update(ass.getGraphics());

            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            ass.update(ass.getGraphics());

            for (int i = 0; i < 100; i++) {
                String a = in.readLine();
                ass.update(ass.getGraphics());

                boolean b = false;
                //    System.out.println(""+a);
                if (a.contains("<title>")) {
                    carpeta = "";
                    int indice = a.indexOf("&");
                    carpeta = a.substring(8, indice - 1);
                    if (carpeta.contains("\\")) {
                        carpeta = carpeta.replace("\\", "");
                    }
                    if (carpeta.contains("/")) {
                        carpeta = carpeta.replace("/", "");
                    }
                    //         System.out.println("TTTT  "+carpeta);
                    }
                ass.update(ass.getGraphics());

                if (a.contains("http://img.submanga.com:8080/pages")) {
                    cadNec = a.substring(63);
                    b = true;
                    break;
                }
            }
            ass.update(ass.getGraphics());

            ass.bajadoo("bajando " + carpeta);
            ass.update(ass.getGraphics());
            //dia.imagenBajandose.setText("BAJANDO"+ carpeta);
//dia.nombreNum.setText("se acaba de crear la carpeta");
//dia.setVisible(true);
            String cadenDes = "http://img.submanga.com:8080/pages/";
            int ca = 0, ba = 0;
            String cop = cadNec;
            ba = cop.indexOf("/");
            String primerNumNec = cadNec.substring(0, ba);
            cop = cop.substring(3);
            ca = cop.indexOf("/");
            //reparar lo de la barra de mas aunque funciona =mente
            String segunNumNec = cadNec.substring(ba + 1, ca + ba + 2);
            if (segunNumNec.contains("/")) {
                //     System.out.println("HA ENTRADOOOOOOOOO");
                //      System.out.println("" + segunNumNec);
                segunNumNec = segunNumNec.substring(0, segunNumNec.length() - 1);
                //    System.out.println("" + segunNumNec);
                }

            for (int i = 1; i < 1000; i++) {
                URL urla = new URL("http://img.submanga.com:8080/pages/" + primerNumNec + "/" + segunNumNec + "/" + i + ".jpg");
//   dia.imagenBajandose.setText("BAJANDO"+ carpeta);
//dia.nombreNum.setText("bajada la imagen "+i+"");

                //   System.out.println(""+ urla);
                URLConnection urlCon = urla.openConnection();
                String compru = urlCon.getContentType();

                //  jTextArea1.setText(jTextArea1.getText() + "\n " + urlCon.getContentType() + " " + i);
                if (compru.contains("Text")) {
                    break;

                }

                ass.update(ass.getGraphics());

                ass.bajadoo("imagen de " + carpeta + "--> " + i);
                ass.update(ass.getGraphics());

                InputStream is = urlCon.getInputStream();
                //  System.out.println("ssss "+carpeta);
                File Carpetaa = new File("" + carpeta);
                Carpetaa.mkdir();
                //    System.out.println(""+Carpetaa.exists());
                //FileOutputStream fos = new FileOutputStream(Carpetaa);
                //      System.out.println("AAAA");
                       FileOutputStream fos ;
                if  (i<10){

                  System.out.println(""+Carpetaa.getAbsolutePath() + "\\" + "0" + i + ".jpg");
                fos = new FileOutputStream(Carpetaa.getAbsolutePath() + "/" + "0" + i + ".jpg");
                }else{

                  System.out.println(""+Carpetaa.getAbsolutePath() + "\\" + "" + i + ".jpg");
                fos = new FileOutputStream(Carpetaa.getAbsolutePath() + "/" + "" + i + ".jpg");
                }

                // sistema.setText("DE " + carpeta + "la imagen 1");

                //    System.out.println("DDDDD");
                byte[] array = new byte[1000]; // buffer temporal de lectura.
                int leido = is.read(array);
                while (leido > 0) {
                    fos.write(array, 0, leido);
                    leido = is.read(array);
                }
                is.close();
                fos.close();
                ass.update(ass.getGraphics());

            }

//dia.nombreNum.setText("BAJADo");
//wait(10);
//dia.setVisible(false);
        } catch (Exception ex) {
        }

    }
}
